using System.Windows.Data;
using Bookvar.ObjectModel;
using Bookvar.ObjectModel.Actions;
using Bookvar.UI.Converters;

namespace Bookvar.UI.ViewModel
{
    public abstract class TopicViewModelBase : ViewModelBase
    {
        private readonly Topic topic;

        protected TopicViewModelBase( Topic topic )
        {
            this.topic = topic;
        }

        public Topic Topic
        {
            get { return topic; }
        }

        private void OnTopicNameChanged( string newName )
        {
            var action = new ChangeTopicAction( Topic ) { Name = newName };
            ActionService.Execute( action );
        }

        public BindingBase CreateTopicNameBinding()
        {
            Binding binding = new Binding( "Name" ) { Source = Topic };
            binding.Converter = new InterceptedBackwardConverter( value => OnTopicNameChanged( value as string ) );

            return binding;
        }

        public BindingBase CreateTopicResourcesBinding()
        {
            Binding binding = new Binding( "Resources" ) { Source = Topic };
            return binding;
        }

        public BindingBase CreateTopicAttachmentsBinding()
        {
            Binding binding = new Binding( "Attachments" ) { Source = Topic };
            return binding;
        }

        public BindingBase CreateTopicHyperlinkBinding()
        {
            //hyperlink is updated explicitly
            Binding binding = new Binding( "Hyperlink" ) { Source = Topic, Mode = BindingMode.OneWay };
            return binding;
        }
    }
}